package com.example.cofeed.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.cofeed.R;
import com.example.cofeed.activity.LoginActivity;
import com.example.cofeed.activity.WebViewActivity;
import com.example.cofeed.api.APIUrl;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.constant.SputilsConstant;
import com.example.cofeed.entity.Article;
import com.example.cofeed.util.TempTimeUtils;
import com.example.cofeed.widget.TagTextView;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import org.apache.commons.lang3.StringEscapeUtils;

import java.util.ArrayList;
import java.util.List;

public class HomeAdapter extends BaseMultiItemQuickAdapter<Article, BaseViewHolder> {
    private boolean noLine;

    public HomeAdapter(List<Article> list) {
        super(list);
//        super(R.layout.adapter_home);
        addItemType(0, R.layout.adapter_home);
        addItemType(1, R.layout.adapter_home_recommend);
        addItemType(2, R.layout.adapter_home_type);
    }

    @Override
    protected void convert(BaseViewHolder helper, Article item) {
        switch (helper.getItemViewType()){
            case 0:
                if (helper.getLayoutPosition() == 0 && !noLine){
                    helper.setVisible(R.id.line_view, true);
                }else {
                    helper.setVisible(R.id.line_view, false);

                }
                TagTextView tvTitle = helper.itemView.findViewById(R.id.tv_title);
                List<String> stringList = new ArrayList<>();

                String Content = getTitle(item.title);

                //热门
                if (item.hot == 1){
                    stringList.add("热点");
                    tvTitle.setContentAndTag(Content, stringList, 1);

                }
                //推荐
                else if (item.vouch == 1){
                    stringList.add("推荐");
                    tvTitle.setContentAndTag(Content, stringList, 0);
                }
                else {
                    tvTitle.setText(Content);
                }

                if (ObjectUtils.isNotEmpty(item.img_src)){
                    helper.setGone(R.id.cv, true);
                }else {
                    helper.setGone(R.id.cv, false);
                }

                RequestOptions options = new RequestOptions()
                        .error(R.mipmap.ic_default)
                        .placeholder(R.mipmap.ic_default);

                Glide.with(mContext).load(item.img_src)
                        .transition(DrawableTransitionOptions.withCrossFade())

                        .apply(options).into((ImageView) helper.itemView.findViewById(R.id.iv_image));
//                helper.setText(R.id.tv_sub_title, item.source + "." + TimeUtils.getFriendlyTimeSpanByNow(item.addtime))
                if (ObjectUtils.isNotEmpty(item.time) && ObjectUtils.isNotEmpty(item.chltitle)){
                    helper.setText(R.id.tv_sub_title, item.chltitle + " . " + TempTimeUtils.getFriendlyTimeSpanByNow(item.time));
                }else if (ObjectUtils.isNotEmpty(item.time)){
                    helper.setText(R.id.tv_sub_title, TempTimeUtils.getFriendlyTimeSpanByNow(item.time));
                }else if (ObjectUtils.isNotEmpty(item.chltitle)){
                    helper.setText(R.id.tv_sub_title, item.chltitle);
                }else {
                    helper.setText(R.id.tv_sub_title, "");
                }
                break;
            case 1:
                HomeRecommendAdapter recommendAdapter = new HomeRecommendAdapter();
                RecyclerView recyclerView = helper.itemView.findViewById(R.id.rv_list);
                recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
                recyclerView.setAdapter(recommendAdapter);
                if (ObjectUtils.isNotEmpty(item.rcm)){
                    recommendAdapter.setNewData(item.rcm);

                }
                recommendAdapter.setOnItemClickListener((adapter, view, position) -> {
                    if (ObjectUtils.isEmpty(SPUtils.getInstance().getString(SputilsConstant.TOKEN))){
                        ActivityUtils.startActivity(LoginActivity.class);
                        return;
                    }
                    Article article = (Article) adapter.getData().get(position);
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    intent.putExtra(IntentKeyConstant.ID, article.id);
                    intent.putExtra(IntentKeyConstant.URL, article.urlpath);
                    mContext.startActivity(intent);
                });
                break;
            case 2:
                helper.itemView.findViewById(R.id.layout_jiage).setOnClickListener(v -> {
                    if (ObjectUtils.isEmpty(SPUtils.getInstance().getString(SputilsConstant.TOKEN))){
                        ActivityUtils.startActivity(LoginActivity.class);
                        return;
                    }
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    intent.putExtra(IntentKeyConstant.TYPE, 2);
                    mContext.startActivity(intent);

                });
                helper.itemView.findViewById(R.id.layout_zhishu).setOnClickListener(v -> {
                    if (ObjectUtils.isEmpty(SPUtils.getInstance().getString(SputilsConstant.TOKEN))){
                        ActivityUtils.startActivity(LoginActivity.class);
                        return;
                    }
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    intent.putExtra(IntentKeyConstant.TYPE, 3);
                    mContext.startActivity(intent);

                });
                helper.itemView.findViewById(R.id.layout_shuju).setOnClickListener(v -> {
                    if (ObjectUtils.isEmpty(SPUtils.getInstance().getString(SputilsConstant.TOKEN))){
                        ActivityUtils.startActivity(LoginActivity.class);
                        return;
                    }
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    intent.putExtra(IntentKeyConstant.TYPE, 4);
                    mContext.startActivity(intent);
                });
                break;
                default:
        }
    }

    private String getTitle(String Content){
        Content = Content.replace("&quot;", "\"\"");
        Content=Content.replace("&acute;", "'");
        Content=Content.replace("&sbquo;", ",");
        Content=Content.replace("&lt;", "<");
        Content=Content.replace("&gt;", ">");
        Content=Content.replace("&#37;", "%");
        Content=Content.replace("&#42;", "*");
        Content=Content.replace("&#43;", "+");
        Content=Content.replace("&#61;", "=");
        Content=Content.replace("&#92;", "\\");
        Content=Content.replace("&#40;", "(");
        Content=Content.replace("&#41;", ")");
        Content=Content.replace("&#123;", "{");
        Content=Content.replace("&#125;", "}");
        Content=Content.replace("&#124;", "|");
        Content=Content.replace("&#63;", "?");
        Content=Content.replace("&xp_cmdshell&;", "xp_cmdshell");
        Content=Content.replace("&net&;", "net");
        Content=Content.replace("&administrators&;", "administrators");
        Content=Content.replace("&select&;", "select");
        Content=Content.replace("&count&;", "count");
        Content=Content.replace("&asc&;", " asc ");
        Content=Content.replace("&char&;", "char");
        Content=Content.replace("&mid&;", "mid");
        Content=Content.replace("&insert&;", "insert");
        Content=Content.replace("&delete&;", "delete");
        Content=Content.replace("&drop&;", "drop");
        Content=Content.replace("&truncate&;", "truncate");
        Content=Content.replace("&update&;", "update");
        Content=Content.replace("&exec&;", "exec");
        Content=Content.replace("&master&;", "master");
        Content=Content.replace("&truncate&;", "truncate");
        Content=Content.replace("&.js&;", ".js");
        Content=Content.replace("&<script&;", "<script");

        return Content;

    }

    public void setNoLine(boolean noLine){
        this.noLine = noLine;
    }
}
